import {SkuCode} from "./sku-code";
import {CellStatus} from "../../core/enum";
import {SkuPending} from "./sku-pending";
import {Joiner} from "../../utils/joiner";

class Judger {
    fenceGroup;
    pathDict = [];
    skuPending;

    constructor(fenceGroup) {
        this.fenceGroup = fenceGroup;
        this._initPathDict();
        this._initSkuPending();
    }

    isSkuIntact() {
        return this.skuPending.isIntact();
    }

    getCurrentValues() {
        return this.skuPending.getCurrentSpecValues();
    }

    getMissingKeys() {
        const missingKeysIndex = this.skuPending.getMissingSpecKeysIndex();
        return missingKeysIndex.map(i => {
            return this.fenceGroup.fences[i].title;
        });
    }

    _initSkuPending() {
        const specsLength = this.fenceGroup.fences.length;
        this.skuPending = new SkuPending(specsLength);
        const defaultSku = this.fenceGroup.getDefaultSku();
        if (!defaultSku) {
            return;
        }
        this.skuPending.init(defaultSku);
        this._initSelectedCell();
        this.judge(null, null, null, true);
        // console.log(this.skuPending);
    }

    _initSelectedCell() {
        this.skuPending.pending.forEach(cell => {
            this.fenceGroup.setCellStatusById(cell.id, CellStatus.SELECTED);
        });
    }

    _initPathDict() {
        this.fenceGroup.spu.sku_list.forEach(s => {
            const skuCode = new SkuCode(s.code);
            this.pathDict = this.pathDict.concat(skuCode.totalSegments);
            console.log(this.pathDict);
        });
    }

    /**
     * 寻找所有可能的sku路径，并且判断这些路径是否存在
     * @param cell
     * @param x
     * @param y
     */
    judge(cell, x, y, isInit = false) {
        if (!isInit) {
            this._changeCurrentCellStatus(cell, x, y);
        }
        this.fenceGroup.eachCell((cell, x, y) => {
            const path = this._findPotentialPath(cell, x, y);
            console.log(path);
            //判断返回来的潜在路径是否为空
            if (!path) {
                return;
            }
            const isIn = this._isInDict(path);
            if (isIn) {
                this.fenceGroup.setCellStatusByXY(x, y, CellStatus.WAITING);
            } else {
                this.fenceGroup.setCellStatusByXY(x, y, CellStatus.FORBIDDEN);
            }
        });
    }

    getDeterminateSku() {
        const code = this.skuPending.getSkuCode();
        const sku = this.fenceGroup.getSku(code);
        return sku;
    }

    _isInDict(path) {
        return this.pathDict.includes(path);
    }

    /**
     * 查找潜在路径
     * cell选择规律：
     *      1、当前的Cell，不需要判断潜在路径。
     *      2、对于某个Cell，它的潜在路径应该是，它自己加上其它行的已选Cell。
     *      3、对于某个Cell，不需要考虑当前行其他Cell是否是已选的。
     * @param cell
     * @param x
     * @param y
     * @private
     */
    _findPotentialPath(cell, x, y) {
        const joiner = new Joiner("#");

        for (let i = 0; i < this.fenceGroup.fences.length; i++) {
            const selected = this.skuPending.findSelectedCellByX(i);
            if (x === i) {
                //当前行
                //cell id 1-42
                //跳过当前行选中的cell
                if (this.skuPending.isSelected(cell, x)) {
                    return;
                }
                const cellCode = this._getCellCode(cell.spec);
                joiner.join(cellCode);
            } else {
                //其它行
                if (selected) {
                    //selected cell path
                    //3-56
                    const selectedCellCode = this._getCellCode(selected.spec);
                    joiner.join(selectedCellCode);
                }
            }
        }
        return joiner.getStr();
    }

    _getCellCode(spec) {
        return spec.key_id + "-" + spec.value_id;
    }

    _changeCurrentCellStatus(cell, x, y) {
        if (cell.status === CellStatus.WAITING) {
            this.fenceGroup.setCellStatusByXY(x, y, CellStatus.SELECTED);
            this.skuPending.insertCell(cell, x);
        }
        if (cell.status === CellStatus.SELECTED) {
            this.fenceGroup.setCellStatusByXY(x, y, CellStatus.WAITING);
            this.skuPending.removeCell(x);
        }
    }

}

export {
    Judger
}